﻿layui.config({
    base: '/Lib/layui/extend/'
}).extend({
    formSelects: 'formSelects-v4'
}).use(['form', 'element', 'layer', 'table', 'laypage', 'laydate', 'jquery'], function () {
    var form = layui.form,
        $ = layui.jquery,
        table = layui.table,
        player = parent.layer || layui.layer,
        laydate = layui.laydate,
        element = layui.element;

    var salaryPaymentListLogic = {
        //初始化
        init: function () {

            self = this;

            this.Parameters = {
                ac: new AsyncCaller(),    //异步方法管理
                title: [],
                type: 11,
                field: 'Name',
                order: 'asc',
                projectAllList: "/Project/Info/LoadProjectAllList",
                listUrl: "/Staff/SalaryPayment/LoadEmployeeSalaryPaymentList",
                subsidyInfoUrl: "/Staff/SalaryPayment/Subsidy",
                revokeSalaryUrl: "/Staff/SalaryPayment/RevokeSalary",
                paymentUrl: "/Staff/SalaryPayment/PaymentSalary",
                exportInfoUrl: "/Staff/SalaryPayment/EpxortSalaryPayment"
            };

            this.Parameters.ac
                .pushQueue(function () {
                    self.logicFunc.loadProjectAllList(function () {
                        self.Parameters.ac.notifyQueueAsyncFuncComplate();
                    });
                })
                .pushQueue(function () {
                    self.initPage();
                    self.Parameters.ac.notifyQueueAsyncFuncComplate();
                })
                .pushQueue(this.bindEvent)
                .exec();

            //this.initPage();
            //this.bindEvent();
        },
        //初始化列表
        initPage: function () {

            //日期
            laydate.render({
                elem: '#YearMothDate',
                format: 'yyyy-MM',
                type: 'month',
                value: new Date()
            });

            self.logicFunc.loadTitleHeader(11);

            //table加载
            GrdLayUI.table({
                dom: '#InfoList',
                url: self.Parameters.listUrl,
                field: self.Parameters.field,
                order: self.Parameters.order,
                height: "full-210",
                parameters: {
                    ProjectNo: $("#ProjectNo").val(),
                    KeyWord: $("#KeyWord").val(),
                    YearMonth: $("#YearMothDate").val(),
                    iType: self.Parameters.type
                },
                title: self.Parameters.title
            });

            $("#funBtnPayment").show();
            $("#funBtnRevoke").hide();

            form.render();
        },
        //绑定事件
        bindEvent: function () {

            element.on('tab(salary)', function (elem) {
                self.logicFunc.loadTitleHeader($(this).attr('lay-id'));
                self.Parameters.type = $(this).attr('lay-id');
                if (self.Parameters.type == 11) {
                    $("#funBtnPayment").show();
                    $("#funBtnRevoke").hide();
                } else {
                    $("#funBtnPayment").hide();
                    $("#funBtnRevoke").show();
                }
                self.logicFunc.loadListInfo(self.Parameters.type);
            });

            //监听(列表操作)
            table.on('tool(InfoList)', function (obj) {
                var data = obj.data;
                switch (obj.event) {
                    case "funBtnPayment":
                        self.logicFunc.payment(data);
                        break;
                    case "funBtnSubsidy":
                        self.logicFunc.subsidy(data);
                        break;
                }
            });
            
            //监听(列表操作)
            table.on('tool(InfoList1)', function (obj) {
                var data = obj.data;
                switch (obj.event) {
                    case "funBtnRevoke":
                        self.logicFunc.revoke(data);
                        break;
                }
            });
            
            //查询
            $("#funBtnSearch").on("click", function () {
                self.logicFunc.loadListInfo(self.Parameters.type);
            });

            //发放
            $("#funBtnPayment").on("click", function () {
                self.logicFunc.payment();
            });

            //撤销工资发放
            $("#funBtnRevoke").on("click", function () {
                self.logicFunc.revoke();
            });

            //导出
            $("#funBtnExport").on("click", function () {
                self.logicFunc.exportInfo();
            });

        },
        /*
         * 逻辑方法
         */
        logicFunc: {

            //获取所有的工程信息
            loadProjectAllList: function (func) {
                GrdLayUI.ajaxPost({
                    url: self.Parameters.projectAllList,
                    data: "",
                    success: function (result) {
                        if (result.Code == 1000) {
                            $("#ProjectNo").empty();
                            $("#ProjectNo").append("<option value=''>所有工程项目</option>");
                            for (var i = 0; i < result.Data.length; i++) {
                                $("#ProjectNo").append("<option value=" + result.Data[i].ProjectNo + ">" + result.Data[i].ProjectName + "</option>");
                            }
                        }
                        if (func) {
                            func();
                        }
                    }
                });
            },
            /**
             * 获取表头
             * */
            loadTitleHeader: function (type) {

                //获取用户信息
                self.Parameters.title =
                    [
                        { type: 'checkbox', fixed: 'left' },
                        { field: 'ID', title: 'ID', hide: true },
                        { field: 'Name', title: '姓名', width: 130, align: "center", fixed: 'left' },
                        { field: 'Sex', title: '性别', width: 80, align: "center", fixed: 'left' },
                        { field: 'WorkTypeName', title: '工种', width: 110, align: "center", fixed: 'left' },
                        { field: 'FNO', title: '工资单号', width: 170, align: "center", fixed: 'left' },
                        { field: 'MyYear', title: '年份', width: 80, align: "center", fixed: 'left' },
                        { field: 'MyMonth', title: '月份', width: 80, align: "center", fixed: 'left' },
                        { field: 'LoanMoney', title: '借款金额', width: 90, align: "center" },
                        { field: 'RealMoney', title: '实发工资', width: 90, align: "center", style: 'background-color: #9DFD05;' },
                        {
                            field: 'WorkDay', title: '考勤天数', width: 90, align: "center", style: 'background-color: #02FECD;',
                            templet: function (d) {
                                return '<a class="" style="color:#ED2A4A">' + d.WorkDay + '</a>';
                            }
                        },
                        {
                            field: 'OverTime', title: '加班小时', width: 90, align: "center",
                            templet: function (d) {
                                return '<a class="" style="color:#35B2FF">' + d.OverTime + '</a>';
                            }
                        },
                        {
                            field: 'TOverMoney', title: '加班工资', width: 90, align: "center",
                            templet: function (d) {
                                return '<a class="" style="color:#35B2FF">' + d.TOverMoney + '</a>';
                            }
                        },
                        {
                            field: 'AmountMoney', title: '计件工资', width: 90, align: "center",
                            templet: function (d) {
                                return '<a class="" style="color:#B883F2">' + d.AmountMoney + '</a>';
                            }
                        },
                        {
                            field: 'RepairDays', title: '调补天数', width: 90, align: "center",
                            templet: function (d) {
                                return '<a class="" style="color:#DC2727">' + d.RepairDays + '</a>';
                            }
                        },
                        {
                            field: 'RepairMoney', title: '调补工资', width: 90, align: "center",
                            templet: function (d) {
                                return '<a class="" style="color:#DC2727">' + d.RepairMoney + '</a>';
                            }
                        },
                        {
                            field: 'CarBT', title: '交通', width: 90, align: "center",
                            templet: function (d) {
                                return '<a class="" style="color:#DC2727">' + d.CarBT + '</a>';
                            }
                        },
                        {
                            field: 'PhoneBT', title: '电话', width: 90, align: "center",
                            templet: function (d) {
                                return '<a class="" style="color:#DC2727">' + d.PhoneBT + '</a>';
                            }
                        },
                        {
                            field: 'BonusBT', title: '奖金', width: 90, align: "center",
                            templet: function (d) {
                                return '<a class="" style="color:#DC2727">' + d.BonusBT + '</a>';
                            }
                        },
                        {
                            field: 'OtherBT1', title: '其他1', width: 90, align: "center",
                            templet: function (d) {
                                return '<a class="" style="color:#DC2727">' + d.OtherBT1 + '</a>';
                            }
                        },
                        {
                            field: 'OtherBT2', title: '其他2', width: 90, align: "center",
                            templet: function (d) {
                                return '<a class="" style="color:#DC2727">' + d.OtherBT2 + '</a>';
                            }
                        },
                        {
                            field: 'PayMethodName', title: '支付方式', width: 90, align: "center",
                            templet: function (d) {
                                return '<a class="" style="color:#DC2727">' + d.PayMethodName + '</a>';
                            }
                        },
                        { field: 'Code', title: '身份证号', width: 170, align: "center" },
                        { field: 'BanKNo', title: '银行账号', width: 170, align: "center" },
                        //{ field: 'Phone', title: '联系电话', width: 120, align: "center" },
                        { field: 'Rmark', title: '备注', width: 160, align: "center" },
                    ];
                if (type == 11) {
                    self.Parameters.title.push({ field: 'opt', title: "操作", toolbar: '#gridTool', width: 170, align: 'left', fixed: 'right' });
                }
                if (type == 22) {
                    self.Parameters.title.push({ field: 'opt', title: "操作", toolbar: '#gridTool', width: 90, align: 'left', fixed: 'right' });
                }
            },
            /**
             * 获取数据
             * */
            loadListInfo: function (type) {

                self.logicFunc.loadTitleHeader(type);

                var domID = "";
                if (type == 11) {
                    domID = "InfoList";
                } else if (type == 22) {
                    domID = "InfoList1";
                } else {
                    domID = "InfoList2";
                }

                //table加载
                GrdLayUI.table({
                    dom: '#' + domID,
                    url: self.Parameters.listUrl,
                    field: self.Parameters.field,
                    order: self.Parameters.order,
                    height: "full-210",
                    parameters: {
                        ProjectNo: $("#ProjectNo").val(),
                        KeyWord: $("#KeyWord").val(),
                        YearMonth: $("#YearMothDate").val(),
                        iType: self.Parameters.type
                    },
                    title: self.Parameters.title
                });
            },

            /**
             * 撤销
             * */
            revoke: function (data) {
                if (data != undefined && data != null) {
                    var json = {
                        parameters: ids
                    };
                    GrdLayUI.ajaxPost({
                        url: self.Parameters.revokeSalaryUrl,
                        data: json,
                        success: function (result) {
                            if (result.Code == 1000) {
                                //刷新父页面
                                self.logicFunc.loadListInfo(self.Parameters.type);
                            } else {
                                GrdLayUI.msg(result.Msg, "警告");
                            }
                        }
                    });
                } else {

                    GrdLayUI.getCheckData({
                        domId: "InfoList1",
                        backAction: function (data) {
                            var ids = [];
                            for (var i = 0; i < data.length; i++) {
                                ids.push(data[i].ID);
                            }
                            var json = {
                                parameters: ids
                            };
                            GrdLayUI.ajaxPost({
                                url: self.Parameters.revokeSalaryUrl,
                                data: json,
                                success: function (result) {
                                    if (result.Code == 1000) {

                                        GrdLayUI.msg(result.Msg, "成功", function () {
                                            //刷新父页面
                                            self.logicFunc.loadListInfo(self.Parameters.type);
                                        });

                                    } else {
                                        GrdLayUI.msg(result.Msg, "警告");
                                    }
                                }
                            });
                        }
                    });
                }
            },
            /**
             * 发放工资
             * @param {any} data
             */
            payment: function (data) {

                if (data != undefined && data != null) {
                    var json = {
                        parameters: ids
                    };
                    GrdLayUI.ajaxPost({
                        url: self.Parameters.paymentUrl,
                        data: json,
                        success: function (result) {
                            if (result.Code == 1000) {
                                //刷新父页面
                                self.logicFunc.loadListInfo(self.Parameters.type);
                            } else {
                                GrdLayUI.msg(result.Msg, "警告");
                            }
                        }
                    });
                } else {
                    GrdLayUI.getCheckData({
                        domId: "InfoList",
                        backAction: function (data) {
                            var ids = [];
                            for (var i = 0; i < data.length; i++) {
                                ids.push(data[i].ID);
                            }
                            var json = {
                                parameters: ids
                            };
                            GrdLayUI.ajaxPost({
                                url: self.Parameters.paymentUrl,
                                data: json,
                                success: function (result) {
                                    if (result.Code == 1000) {

                                        GrdLayUI.msg(result.Msg, "成功", function () {
                                            //刷新父页面
                                            self.logicFunc.loadListInfo(self.Parameters.type);
                                        });

                                    } else {
                                        GrdLayUI.msg(result.Msg, "警告");
                                    }
                                }
                            });
                        }
                    });
                }
            },
            /**
             * 修改补贴
             * @param {any} data
             */
            subsidy: function (data) {

                if (data != undefined && data != null) {

                    GrdLayUI.setUrlParameters(data);

                    var index = layui.layer.open({
                        title: "修改补贴",
                        type: 2,
                        resize: false,
                        area: ['500px', '400px'],
                        content: self.Parameters.subsidyInfoUrl,
                        end: function () {
                            $(window).unbind("resize");
                        }
                    });
                }
            },
            //导出信息
            exportInfo: function () {
                var json = {
                    parameters: {
                        ProjectNo: $("#ProjectNo").val(),
                        KeyWord: $("#KeyWord").val(),
                        YearMonth: $("#YearMothDate").val(),
                        iType: self.Parameters.type
                    }
                };
                GrdLayUI.ajaxPost({
                    url: self.Parameters.exportInfoUrl,
                    data: json,
                    success: function (result) {
                        if (result.Code == 1000) {
                            window.open("http://" + location.host + result.Msg, "_self");
                        }
                    }
                });
            },
        },
        //工具
        tools: {

        }
    };
    salaryPaymentListLogic.init();
});
